package com.dongdongshop.service.impl;

import com.dongdongshop.pojo.TbGoods;
import com.dongdongshop.pojo.TbGoodsDesc;
import com.dongdongshop.pojo.TbItem;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.service.*;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ItemPageServiceImpl implements ItemPageService {
    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    @DubboReference
    private TbGoodsService tbGoodsService;

    @DubboReference
    private TbGoodsDescService tbGoodsDescService;

    @DubboReference
    private TbItemService tbItemService;

    @DubboReference
    private TbItemCatService tbItemCatService;

    @Value("${itempage.outPath}")
    private String outPath;

    @Override
    public void itemPage(Long goodsId) {
//        第一步：创建一个 Configuration 对象，直接 new 一个对象。构造方法的参数就是 freemarker的版本号。
        Configuration configuration = freeMarkerConfigurer.getConfiguration();
        FileWriter fileWriter = null;
        try {
//        第四步：加载一个模板，创建一个模板对象。
            Template template = configuration.getTemplate("item.ftl");
            /*通过dubbo调用查询goods商品数据更具id*/
            TbGoods tbGoods = tbGoodsService.selectByGoodsId(goodsId);
            TbGoodsDesc tbGoodsDesc = tbGoodsDescService.selectByGoodsId(goodsId);
            List<TbItem> itemList = tbItemService.selectItemListByGoodsId(goodsId);
            TbItemCat tbItemCat1 = tbItemCatService.selectByParentId(tbGoods.getCategory1Id());
            TbItemCat tbItemCat2 = tbItemCatService.selectByParentId(tbGoods.getCategory2Id());
            TbItemCat tbItemCat3 = tbItemCatService.selectByParentId(tbGoods.getCategory3Id());
//        第五步：创建一个模板使用的数据集，可以是 pojo 也可以是 map。一般是 Map。
            Map map = new HashMap();
            map.put("tbGoods",tbGoods);
            map.put("tbGoodsDesc",tbGoodsDesc);
            map.put("itemList",itemList);
            map.put("category1Id",tbItemCat1.getName());
            map.put("category2Id",tbItemCat2.getName());
            map.put("category3Id",tbItemCat3.getName());
//         第六步：创建一个 Writer 对象，一般创建一 FileWriter 对象，指定生成的文件名。
            fileWriter = new FileWriter(outPath + goodsId + ".html");
//            第七步：调用模板对象的 process 方法输出文件。
            template.process(map,fileWriter);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (fileWriter != null){
                try {
                    fileWriter.close();
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
            }
        }
    }
}
